package de.lmu.ifi.pst.uwejsf.framework;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

public class UWENavigationActionListener implements ActionListener {

	@Override
	public void processAction(ActionEvent ev) throws AbortProcessingException {
		UIComponent component = ev.getComponent();
		Map<String, Object> attributes = component.getAttributes();

		AbstractUWENavigator navigator = AbstractUWENavigator.getNavigator();

		navigator.setCurrentContentInLink(getContentInLink(attributes));
		navigator.setCurrentLinks(getLinks(attributes));
		navigator.setCurrentTargetBeans(getTargetBeans(attributes));
		navigator.setCurrentSourceView(getSourceView(attributes));
		navigator.setCurrentTargetView(getTargetView(attributes));

	}

	protected String getTargetView(Map<String, Object> attributes) {
		Object o = attributes.get("targetView");
		if (o == null)
			return null;
		return (String) o;

	}

	protected String getSourceView(Map<String, Object> attributes) {
		Object o = attributes.get("sourceView");
		if (o == null)
			return null;
		return (String) o;

	}

	protected List<String> getLinks(Map<String, Object> attributes) {
		Vector<String> links = new Vector<String>();

		int num = 1;
		while (attributes.get("link_" + num) != null) {
			links.add((String) attributes.get("link_" + num));
			num++;
		}
		return links;
	}

	protected List<String> getTargetBeans(Map<String, Object> attributes) {
		Vector<String> targetBeans = new Vector<String>();

		int num = 1;
		while (attributes.get("targetBean_" + num) != null) {
			targetBeans.add((String) attributes.get("targetBean_" + num));
			num++;
		}
		return targetBeans;
	}

	protected String getContentInLink(Map<String, Object> attributes) {
		Object o = attributes.get("contentInLink");
		if (o == null)
			return null;
		return (String) o;
	}

}
